/*
 * Copyright (c) 2008 SQLI http://www.sqli.com/
 * Author: Olivier CARPENTIER
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SQLI.WSS.WebControls.DataSource
{
    /// <summary>
    /// Configuration form for the DataSource's where parameters.
    /// </summary>
    public partial class ConfigWSSListDataSourceWhere : Form
    {
        private WhereParameterCollection whereParameters = new WhereParameterCollection();

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigWSSListDataSourceWhere"/> class.
        /// </summary>
        public ConfigWSSListDataSourceWhere()
        {
            InitializeComponent();
        }

        private WSSListDataSource _component;
        /// <summary>
        /// Gets or sets the component data source.
        /// </summary>
        /// <value>The component data source.</value>
        public WSSListDataSource ComponentDataSource
        {
            get { return _component; }
            set { _component = value; }
        }

        /// <summary>
        /// Sets the fields.
        /// </summary>
        /// <param name="fields">The fields.</param>
        public void SetFields(List<string> fields)
        {
            this.comboBoxField.Items.Clear();
            foreach (string field in fields)
            {
                this.comboBoxField.Items.Add(field);
            }

            if (this.comboBoxField.Items.Count > 0)
                this.comboBoxField.SelectedIndex = 0;
            this.comboBoxOperator.SelectedIndex = 0;
            this.comboBoxSource.SelectedIndex = 0;

            if (this.ComponentDataSource != null)
            {
                this.whereParameters.Clear();
                this.whereParameters.AddRange(this.ComponentDataSource.WhereParameters);
                Redraw();
            }
        }

        /// <summary>
        /// Redraws this instance.
        /// </summary>
        private void Redraw()
        {
            this.listViewClauses.Items.Clear();
            foreach (WhereParameter param in this.whereParameters)
                this.listViewClauses.Items.Add(param.ToClause());
        }

        /// <summary>
        /// Handles the Click event of the buttonOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.ComponentDataSource.WhereParameters.Clear();
            this.ComponentDataSource.WhereParameters.AddRange(this.whereParameters);

            this.Close();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the comboBoxSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void comboBoxSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.groupBoxParameter.Enabled = true;
            if (this.comboBoxSource.SelectedIndex == 0)
            {
                //None
                this.panelParameter2.Visible = false;
                this.labelParamName.Text = "Value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 1)
            {
                //Control
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "Control ID:";
                this.labelParamValue.Text = "Default value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 2)
            {
                //Cookie
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "Cookie name:";
                this.labelParamValue.Text = "Default value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 3)
            {
                //Form
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "Form field:";
                this.labelParamValue.Text = "Default value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 4)
            {
                //Profile
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "Property name:";
                this.labelParamValue.Text = "Default value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 5)
            {
                //QueryString
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "QueryString field:";
                this.labelParamValue.Text = "Default value:";
            }
            else if (this.comboBoxSource.SelectedIndex == 6)
            {
                //Session
                this.panelParameter2.Visible = true;
                this.labelParamName.Text = "Session field:";
                this.labelParamValue.Text = "Default value:";
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonAddOR control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonAddOR_Click(object sender, EventArgs e)
        {
            if (this.comboBoxField.Text == string.Empty)
            {
                MessageBox.Show("Please enter a valid Field name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (this.textBoxParameterName.Text == string.Empty)
            {
                MessageBox.Show("Please enter a valid value.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            WhereParameter whereParam = new WhereParameter();
            whereParam.Value = this.textBoxParameterName.Text;
            whereParam.DefaultValue = this.textBoxParameterValue.Text;

            if (this.comboBoxSource.SelectedIndex == 0)
            {
                //None
                whereParam.ParameterType = ParameterType.Value;
            }
            else if (this.comboBoxSource.SelectedIndex == 1)
            {
                //Control
                whereParam.ParameterType = ParameterType.Control;
            }
            else if (this.comboBoxSource.SelectedIndex == 2)
            {
                //Cookie
                whereParam.ParameterType = ParameterType.Cookie;
            }
            else if (this.comboBoxSource.SelectedIndex == 3)
            {
                //Form
                whereParam.ParameterType = ParameterType.Form;
            }
            else if (this.comboBoxSource.SelectedIndex == 4)
            {
                //Profile
                whereParam.ParameterType = ParameterType.Profil;
            }
            else if (this.comboBoxSource.SelectedIndex == 5)
            {
                //QueryString
                whereParam.ParameterType = ParameterType.QueryString;
            }
            else if (this.comboBoxSource.SelectedIndex == 6)
            {
                //Session
                whereParam.ParameterType = ParameterType.Session;
            }
            whereParam.FieldName = this.comboBoxField.Text;
            whereParam.OperatorType = (OperatorType)this.comboBoxOperator.SelectedIndex;
            this.listViewClauses.Items.Add(whereParam.ToClause());
            whereParameters.Add(whereParam);
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.listViewClauses.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
                int indexToRemove = col[0];
                this.listViewClauses.Items.RemoveAt(indexToRemove);
                whereParameters.RemoveAt(indexToRemove);
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonMoveUp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonMoveUp_Click(object sender, EventArgs e)
        {
            if (this.listViewClauses.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
                int indexToMove = col[0];
                if (indexToMove > 0)
                {
                    WhereParameter moved = whereParameters[indexToMove];
                    WhereParameter prev = whereParameters[indexToMove - 1];
                    whereParameters[indexToMove - 1] = moved;
                    whereParameters[indexToMove] = prev;
                    this.listViewClauses.Items[indexToMove - 1].Selected = true;
                    this.listViewClauses.Refresh();
                }
                Redraw();
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonMoveDown control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonMoveDown_Click(object sender, EventArgs e)
        {
            if (this.listViewClauses.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
                int indexToMove = col[0];
                if (indexToMove < this.listViewClauses.Items.Count - 1)
                {
                    WhereParameter moved = whereParameters[indexToMove];
                    WhereParameter next = whereParameters[indexToMove + 1];
                    whereParameters[indexToMove + 1] = moved;
                    whereParameters[indexToMove] = next;
                    this.listViewClauses.Items[indexToMove + 1].Selected = true;
                    this.listViewClauses.Refresh();
                }
                Redraw();
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the listViewClauses control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void listViewClauses_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
            if (col.Count > 0)
            {
                int indexParameter = col[0];
                if (indexParameter == 0)
                    this.buttonMoveUp.Enabled = false;
                else
                    this.buttonMoveUp.Enabled = true;
                if (indexParameter == this.whereParameters.Count - 1)
                    this.buttonMoveDown.Enabled = false;
                else
                    this.buttonMoveDown.Enabled = true;

                //Display current clause
                DisplayWhereParameter(indexParameter);
            }
            else
            {
                this.buttonMoveUp.Enabled = false;
                this.buttonMoveDown.Enabled = false;
            }
        }

        private void DisplayWhereParameter(int indexParameter)
        {
            WhereParameter whereParam = this.whereParameters[indexParameter];
            this.textBoxParameterName.Text = whereParam.Value;
            this.textBoxParameterValue.Text = whereParam.DefaultValue;
            this.comboBoxField.Text = whereParam.FieldName;
            this.comboBoxOperator.SelectedIndex = (int)whereParam.OperatorType;
            this.comboBoxSource.SelectedIndex = (int)whereParam.ParameterType;
        }

        private void comboBoxField_TextChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
            if (col.Count > 0)
            {
                int indexParameter = col[0];
                WhereParameter whereParam = this.whereParameters[indexParameter];
                whereParam.FieldName = comboBoxField.Text;
                Redraw();
            }
        }

        private void comboBoxOperator_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
            if (col.Count > 0)
            {
                int indexParameter = col[0];
                WhereParameter whereParam = this.whereParameters[indexParameter];
                whereParam.OperatorType = (OperatorType)comboBoxOperator.SelectedIndex;
                Redraw();
            }
        }

        private void textBoxParameterName_TextChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
            if (col.Count > 0)
            {
                int indexParameter = col[0];
                WhereParameter whereParam = this.whereParameters[indexParameter];
                whereParam.Value = textBoxParameterName.Text;
                Redraw();
            }
        }

        private void textBoxParameterValue_TextChanged(object sender, EventArgs e)
        {
            ListView.SelectedIndexCollection col = this.listViewClauses.SelectedIndices;
            if (col.Count > 0)
            {
                int indexParameter = col[0];
                WhereParameter whereParam = this.whereParameters[indexParameter];
                whereParam.DefaultValue = textBoxParameterValue.Text;
                Redraw();
            }
        }

    }
}